(function () {
    $(".proCitySelAll_Img").click(function (event) {
        event.stopPropagation();
        $(this).prev().trigger("click");
    });
})();
function wrongMsg(object, msg) {
    $(".wrongMsg").text(msg);
    object.addClass("wrong");
}
(function () {
    var picNum = $("div.flashPic img").size();
    var isNum = 0;
    var str = [];
    var imgDiv = $("div.flashPic");
    var $div = $("div.picNum");
    imgDiv.find("img").hide().eq(0).show();
    for (var i = 0; i < picNum; i++) {
        str[i] = "<a href='javascript:'>" + (i + 1) + "</a>";
    }
    $div.html(str.join(""));
    $div.find("a:eq(0)").addClass("on");
    function MovePic() {
        if ((isNum + 1) >= picNum) {
            isNum = 0;
        }
        else {
            isNum = isNum + 1;
        }
        imgDiv.find("img").hide().eq(isNum).fadeIn(500);
        $div.find("a").removeClass("on").eq(isNum).addClass("on");
    }

    var setFn = setInterval(MovePic, 4000);
    $div.find("a").click(function () {
        clearInterval(setFn);
        var j = $(this).index();
        $div.find("a").removeClass("on").siblings().eq(j).addClass("on");
        imgDiv.find("img").hide();
        imgDiv.find("img").eq(j).fadeIn(500);
        isNum = j;
        setFn = setInterval(MovePic, 4000);
    });
})();

(function () {
    var clkIndex;
    var currentClass;
    var allCitys;
    var allProvinces;
    var allCountys;
    var thisObj;
    var dimCityDiv = "<div id='dimCityQuery'><ul></ul></div>";
    $("body").append(dimCityDiv);
    $("body").delegate(".proCityQuery,.proCityQueryAll", ($.support.opera ? "keypress" : "keyup"),
        function (event) {
            if ($("#dimCityQuery:visible").size() == 0) {
                $(".backifname").hide();
            }
            $(".provinceCity").hide();
            $(".provinceCityAll").hide();
            if ($(this).hasClass("proCityQueryAll")) {
                if ($("body").data("allProvinces") == null) {
                    sendAllProvinceAjax();
                }
                if ($("body").data("CitysAll") == null) {
                    sendAllCitiesAjax();
                }
                if ($("body").data("allCountys") == null) {
                    sendAllCountiesAjax();
                }
                currentClass = "proCityQueryAll";
                clkIndex = $("body").find(".proCityQueryAll").index(this);
                allCitys = $("body").data("CitysAll");
                allProvinces = $("body").data("allProvinces");
                allCountys = $("body").data("allCountys");
                thisObj = $(this);
            }
            if ($(this).hasClass("proCityQuery")) {
                if ($("body").data("allExistProvinces") == null) {
                    sendAllProvinceAjax();
                }
                if ($("body").data("allExistCitys") == null) {
                    sendAllCitiesAjax();
                }
                if ($("body").data("allExistCountys") == null) {
                    sendAllCountiesAjax();
                }
                currentClass = "proCityQuery";
                clkIndex = $("body").find(".proCityQuery").index(this);
                allCitys = $("body").data("allExistCitys");
                allProvinces = $("body").data("allExistProvinces");
                allCountys = $("body").data("allExistCountys");
                thisObj = $(this);
            }
            lastKeyPressCode = event.keyCode;
            switch (lastKeyPressCode) {
                case 40:
                    $("#dimCityQuery").trigger("selNext");
                    return false;
                    break;
                case 38:
                    $("#dimCityQuery").trigger("selPrev");
                    return false;
                    break;
                case 13:
                    $("#dimCityQuery").trigger("enter");
                    return false;
                    break;
            }
            v = $.trim($(this).val());
            if (v == "" || v == null) {
                return false;
            }
            $(".provinceCity").hide();
            var o = $(this).offset();
            var l = o.left;
            var t = o.top;
            var w = $(this).width();
            var h = $(this).height();
            var htmlArr = [];
            var autoWidth;
            for (i = 0; i < allCountys.length; i++) {
                if (v.toUpperCase() === allCountys[i].pinYinChar.substring(0, v.length)) {
                    htmlArr[htmlArr.length] = "<li><a class='allcityClass' href='javascript:' provinceId=" + allCountys[i].provinceId + " cityId=" + allCountys[i].cityId + " countyId=" + allCountys[i].id + ">" + allCountys[i].cityName + "-" + allCountys[i].areaName + " (<span style='color:red'>" + v.toUpperCase() + "</span>" + allCountys[i].pinYinChar.substring(v.length, allCountys[i].pinYinChar.length) + ")</a></li>";
                    if (htmlArr.length > 9) {
                        break;
                        return false;
                    }
                    autoWidth = autoWidth < (allCountys[i].cityName + allCountys[i].areaName + allCountys[i].pinYinChar).length ? (allCountys[i].cityName + allCountys[i].areaName + allCountys[i].pinYinChar).length : autoWidth;
                    continue;
                }
                ;
                if (v === allCountys[i].areaName.substring(0, v.length)) {
                    htmlArr[htmlArr.length] = "<li><a class='allcityClass' href='javascript:' provinceId=" + allCountys[i].provinceId + " cityId=" + allCountys[i].cityId + " countyId=" + allCountys[i].id + ">" + allCountys[i].cityName + "-" + "<span style='color:red'>" + v + "</span>" + allCountys[i].areaName.substring(v.length, allCountys[i].areaName.length) + " (" + allCountys[i].pinYinChar + ")</a></li>";
                    if (htmlArr.length > 9) {
                        break;
                        return false;
                    }
                    autoWidth = autoWidth < (allCountys[i].cityName + allCountys[i].areaName + allCountys[i].pinYinChar).length ? (allCountys[i].cityName + allCountys[i].areaName + allCountys[i].pinYinChar).length : autoWidth;
                    continue;
                }
                ;
                if (v.toLowerCase() === allCountys[i].pinYin.substring(0, v.length)) {
                    htmlArr[htmlArr.length] = "<li><a class='allcityClass' href='javascript:' provinceId=" + allCountys[i].provinceId + " cityId=" + allCountys[i].cityId + " countyId=" + allCountys[i].id + ">" + allCountys[i].cityName + "-" + allCountys[i].areaName + " (<span style='color:red'>" + v.toLowerCase() + "</span>" + allCountys[i].pinYin.substring(v.length, allCountys[i].pinYin.length) + ")</a></li>"
                    if (htmlArr.length > 9) {
                        break;
                        return false;
                    }
                    autoWidth = autoWidth < (allCountys[i].cityName + allCountys[i].areaName + allCountys[i].pinYin).length ? (allCountys[i].cityName + allCountys[i].areaName + allCountys[i].pinYin).length : autoWidth;
                    continue;
                }
                ;
            }
            for (i = 0; i < allCitys.cities.length; i++) {
                if (v.toUpperCase() === allCitys.cities[i].cityShortPY.substring(0, v.length)) {
                    htmlArr[htmlArr.length] = "<li><a class='allcityClass' href='javascript:' provinceId=" + allCitys.cities[i].provinceId + " cityId=" + allCitys.cities[i].id + ">" + allCitys.cities[i].name + " (<span style='color:red'>" + v.toUpperCase() + "</span>" + allCitys.cities[i].cityShortPY.substring(v.length, allCitys.cities[i].cityShortPY.length) + ")</a></li>";
                    if (htmlArr.length > 9) {
                        break;
                        return false;
                    }
                    autoWidth = autoWidth < (allCitys.cities[i].name + allCitys.cities[i].cityShortPY).length ? (allCitys.cities[i].name + allCitys.cities[i].cityShortPY).length : autoWidth;
                    continue;
                }
                ;
                if (v === allCitys.cities[i].name.substring(0, v.length)) {
                    htmlArr[htmlArr.length] = "<li><a class='allcityClass' href='javascript:' provinceId=" + allCitys.cities[i].provinceId + " cityId=" + allCitys.cities[i].id + "><span style='color:red'>" + v + "</span>" + allCitys.cities[i].name.substring(v.length, allCitys.cities[i].name.length) + " (" + allCitys.cities[i].cityShortPY + ")</a></li>";
                    if (htmlArr.length > 9) {
                        break;
                        return false;
                    }
                    autoWidth = autoWidth < (allCitys.cities[i].name + allCitys.cities[i].cityShortPY).length ? (allCitys.cities[i].name + allCitys.cities[i].cityShortPY).length : autoWidth;
                    continue;
                }
                ;
                if (v.toLowerCase() === allCitys.cities[i].cityPinyin.substring(0, v.length)) {
                    htmlArr[htmlArr.length] = "<li><a class='allcityClass' href='javascript:' provinceId=" + allCitys.cities[i].provinceId + " cityId=" + allCitys.cities[i].id + ">" + allCitys.cities[i].name + " (<span style='color:red'>" + v.toLowerCase() + "</span>" + allCitys.cities[i].cityPinyin.substring(v.length, allCitys.cities[i].cityPinyin.length) + ")</a></li>"
                    if (htmlArr.length > 9) {
                        break;
                        return false;
                    }
                    autoWidth = autoWidth < (allCitys.cities[i].name + allCitys.cities[i].cityPinyin).length ? (allCitys.cities[i].name + allCitys.cities[i].cityPinyin).length : autoWidth;
                    continue;
                }
                ;
            }
            ;
            if (htmlArr == "" || htmlArr == null) {
                $("#dimCityQuery ul").html("<li class='none'>对不起,没有找到该城市</li>");
                return false;
            } else {
                $("#dimCityQuery ul").html(htmlArr.join("")).find("li:first").addClass("current");
            }
            ;
            if (autoWidth < 200) {
                autoWidth = 200;
            }
            $("#dimCityQuery").css("width", autoWidth).css("top", t + h - 1).css("left", l).show();
            $(".backifname").show();
            $("html").click(function () {
                $("#dimCityQuery").hide();
                $(".backifname").hide();
            });
        });
    $("body").delegate("#dimCityQuery li", "hover",
        function () {
            $(this).addClass("current").siblings().removeClass("current");
        },
        function () {
            $(this).removeClass("current");
        });
    $("#dimCityQuery").delegate("", "selNext",
        function () {
            var next = $(this).find("li.current").next();
            if (next.size() > 0) {
                next.addClass("current").siblings().removeClass("current");
            }
            else {
                $("#dimCityQuery li").removeClass("current").first().addClass("current");
            }
            ;
        });
    $("#dimCityQuery").delegate("", "selPrev",
        function () {
            var prev = $(this).find("li.current").prev();
            if (prev.size() > 0) {
                prev.addClass("current").siblings().removeClass("current");
            }
            else {
                $("#dimCityQuery li").removeClass("current").last().addClass("current");
            }
            ;
        });
    $("#dimCityQuery").delegate("", "enter",
        function (event) {
            var cur = $(this).find("li.current");
            if (cur.size() > 0) {
                cur.find("a").trigger("click");
            }
            ;
        });
    $("body").delegate("#dimCityQuery li a.allcityClass", "click",
        function () {
            var vm = $(this).text();
            var provinceId = $(this).attr("provinceId");
            var cityId = $(this).attr("cityId");
            var countyId = $(this).attr("countyId");
            var provinceName;
            var cityName;
            var rtn;
            for (i = 0; i < allProvinces.length; i++) {
                if (allProvinces[i].id == provinceId) {
                    provinceName = allProvinces[i].provinceName;
                }
                ;
            }
            for (i = 0; i < allCitys.cities.length; i++) {
                if (allCitys.cities[i].id == cityId) {
                    cityName = allCitys.cities[i].name;
                }
            }
            if (currentClass == "proCityQueryAll") {
                $("body").data("pAllId", provinceId);
                $("body").data("cAllId", cityId);
                $("body").data("aAllId", countyId);
                $("body").data("pAllName", provinceName);
                $("body").data("nameOfCityAll", cityName);
            }
            if (currentClass == "proCityQuery") {
                $("body").data("pId", provinceId);
                $("body").data("cId", cityId);
                $("body").data("aId", countyId);
                $("body").data("pName", provinceName);
                $("body").data("nameOfCity", cityName);
            }
            vm = vm.split("(");
            countyName = $.trim(vm[0]);
            if (countyId == null || countyName == cityName) {
                if (currentClass == "proCityQuery") {
                    thisObj.trigger("click");
                    counties = [];
                    var j = 0;
                    $.each(allCountys,
                        function (i, county) {
                            if (county.cityId == cityId) {
                                counties[j++] = county;
                            }
                        });
                    countyTotalPage = Math.ceil(counties.length / p_pageSize);
                    $(".provinceCity").find(".tabs").find("a").removeClass("current");
                    $(".provinceCity .tabs").find("#county").addClass("current");
                    $(".con .city .list a").removeClass("current");
                    $(".provinceCity").find(".con").children().hide();
                    $(".provinceCity").find(".con").find(".county").show();
                    $(".con .provinceAll .list a").removeClass("current");
                    countyPage(1);
                }
                else if (currentClass == "proCityQueryAll") {
                    thisObj.trigger("click");
                    countiesAll = [];
                    var j = 0;
                    $.each(allCountys,
                        function (i, county) {
                            if (county.cityId == cityId && county.areaName != cityName) {
                                countiesAll[j++] = county;
                            }
                        });
                    countyTotalPageAll = Math.ceil(countiesAll.length / p_pageSize);
                    $(".provinceCityAll").find(".tabs").find("a").removeClass("current");
                    $(".provinceCityAll .tabs").find("#countyAll").addClass("current");
                    $(".con .cityAll .list a").removeClass("current");
                    $(".provinceCityAll").find(".con").children().hide();
                    $(".provinceCityAll").find(".con").find(".countyAll").show();
                    $(".con .provinceAll .list a").removeClass("current");
                    allCountyPage(1);
                }
            }
            else {
                rtn = provinceName + "-" + countyName;
                if (currentClass == "proCityQueryAll") {
                    $("body").find(".proCityQueryAll").eq(clkIndex).val(rtn);
                    $("body").find(".proCityQueryAll").eq(clkIndex).trigger("change");
                    $(".provinceCityAll").find(".tabs").find("a").removeClass("current");
                    $(".provinceCityAll").find(".tabs").find("a[tb=hotCityAll]").addClass("current");
                    $(".provinceCityAll .con .list a").removeClass("current");
                    $(".provinceCityAll .con .list a input").removeClass("current");
                }
                if (currentClass == "proCityQuery") {
                    $("body").find(".proCityQuery").eq(clkIndex).val(rtn);
                    $("body").find(".proCityQuery").eq(clkIndex).trigger("change", [cityId, countyId]);
                    $(".provinceCity").find(".tabs").find("a").removeClass("current");
                    $(".provinceCity").find(".tabs").find("a[tb=hotCity]").addClass("current");
                    $(".provinceCity .con .list a").removeClass("current");
                    $(".provinceCity .con .list a input").removeClass("current");
                }
            }
            $("#dimCityQuery").hide();
            $(".backifname").hide();
            return false;
        });
    $(".nomarl").live("focus",
        function () {
            var ov = $.trim($(this).attr("ov"));
            var val = $.trim($(this).val());
            $(this).css({
                "color": "#000"
            });
            if (val == ov) {
                $(this).val("");
            }
        });
    $(".nomarl").live("blur",
        function () {
            var ov = $.trim($(this).attr("ov"));
            var val = $.trim($(this).val());
            if (val == "" || val == ov) {
                $(this).val(ov).css({
                    "color": "#aaa"
                });
            }
        });
})();

function viewHotCities() {
    $.each(cities,
        function (i, city) {
            if (city.hotCity) {
                $(".hotCity .list ul").append("<li><a><input type='button' style='background:none;border:0px;cursor: pointer;' onclick=hotCityAddrInput(\'" + city.provinceId + "," + city.id + "," + city.name + "\') id='" + city.id + "' value='" + city.name + "'></a></li>");
            }
        });
}
$(".province .pre a").bind('click',
    function () {
        var provincePage = parseInt($('#provincePage').html());
        if (provincePage == 1) {
            return;
        }
        viewProvince(provincePage - 1);
    });
$(".city .pre a").bind('click',
    function () {
        var cityPages = parseInt($('#cityPage').html());
        if (cityPages == 1) {
            return;
        }
        cityPage(cityPages - 1);
    });
$(".county .pre a").bind('click',
    function () {
        var countyPages = parseInt($('#countyPage').html());
        if (countyPages == 1) {
            return;
        }
        countyPage(countyPages - 1);
    });
$(".province .next a").bind('click',
    function () {
        var provincePage = parseInt($('#provincePage').html());
        provinceTotalPage = countProvincePages();
        if (provincePage == provinceTotalPage) {
            return;
        }
        viewProvince(provincePage + 1);
    });
$(".city .next a").bind('click',
    function () {
        if ($(this).hasClass("can")) {
            var cityPages = parseInt($('#cityPage').html());
            cityPage(cityPages + 1);
        }
    });
$(".county .next a").bind('click',
    function () {
        if ($(this).hasClass("can")) {
            var countyPages = parseInt($('#countyPage').html());
            countyPage(countyPages + 1);
        }
    });
function json2str(o) {
    var arr = [];
    var fmt = function (s) {
        if (typeof s == 'object' && s != null) return json2str(s);
        return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
    };
    for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
    return '{' + arr.join(',') + '}';
}
//var provinceTotalPage=null;
//var p_pageSize = 12;
//var provinces=[];
//var citys=[];
function countProvincePages() {
    provinceTotalPage = Math.ceil(provinces.length / p_pageSize);
    return provinceTotalPage;
}

function viewProvince(page) {
    p_pageSize=provinces.length;
    $(".province .list ul li").remove();
    if (page == 1) {
        $(".province .pre a").removeClass("can");
        $(".province .next a").addClass("can");
    } else {
        $(".province .pre a").addClass("can");
        $(".province .next a").addClass("can");
    }
    var end;
    var start;
    if (page == provinceTotalPage) {
        start = (page - 1) * p_pageSize;
        end = provinces.length;
        $(".province .next a").removeClass("can");
    } else {
        start = (page - 1) * p_pageSize;
        end = start + p_pageSize;
    }
    $(".provinceAll .list ul").empty();
    for (var i = start; i < end; i++) {
        var p_id = provinces[i].id;
        var p_name = provinces[i].provinceName;
        if (provinces[i].provinceName == '内蒙古自治区') {
            p_name = '内蒙古';
        } else if (provinces[i].provinceName == '黑龙江省') {
            p_name = '黑龙江';
        } else {
            p_name = provinces[i].provinceName.substr(0, 2);
        }
        var li = $('<li><a style="background: none repeat scroll 0% 0% transparent; border: 0px none;" href="javascript:onclick=viewAllCities1(' + i + ');" id="' + p_id + '">' + p_name + '</a></li>');
        $(".province .list ul").append(li);
    }
    $(".province .list #provincePage").remove();
    $(".province .list").append("<label id='provincePage' style='display:none;'>" + page + "</label>");
}

function viewCities(i) {
    proId = provinces[i].id;
    $("body").data("pName", provinces[i].provinceName);
    $("body").data("pId", proId);
    citys = [];
    var j = 0;
    $.each(citys,
        function (i, city) {
            if (city.provinceId == proId) {
                citys[j++] = city;
            }
        });
    cityTotalPage = Math.ceil(citys.length / p_pageSize);
    $(".provinceCity").find(".tabs").find("a").removeClass("current");
    $(".provinceCity .tabs").find("#city").addClass("current");
    $(".con .province .list a").removeClass("current");
    $(".con .province .list a[id='" + proId + "']").addClass("current");
    $(".provinceCity").find(".con").children().hide();
    $(".provinceCity").find(".con").find(".city").show();
    cityPage(1);
}
function cityPage(page) {
    $(".city .list ul li").remove();
    $(".cityAll .list ul li").remove();
    if (page == 1) {
        $(".city .pre a").removeClass("can");
    } else {
        $(".city .pre a").addClass("can");
    }
    var start;
    var end;
    if (page <= 1) {
        page = 1;
        $(".city .pre a").removeClass("can");
        $(".city .next a").addClass("can");
    }
    if (cityTotalPage == 1) {
        $(".city .next a").removeClass("can");
        $(".city .pre a").removeClass("can");
    }
    if (page >= cityTotalPage) {
        page = cityTotalPage;
        $(".city .next a").removeClass("can");
        start = (page - 1) * p_pageSize;
        end = citys.length;
    } else if (page == 1) {
        start = (page - 1) * p_pageSize;
        end = start + p_pageSize;
        $(".city .pre a").removeClass("can");
        $(".city .next a").addClass("can");
    } else {
        start = (page - 1) * p_pageSize;
        end = start + p_pageSize;
        $(".city .next a").addClass("can");
        $(".city .pre a").addClass("can");
    }
    for (var i = start; i < end; i++) {
        var c_id = citys[i].id;
        var cityName = citys[i].name.substr(0, 4);
        var li = $('<li><a href="javascript:onclick=viewCounties(' + i + ')" id="' + c_id + '">' + cityName + '</a></li>');
        $(".city .list ul").append(li);
    }
    $(".city .list #cityPage").remove();
    $(".city .list").append("<label id='cityPage' style='display:none;'>" + page + "</label>");
}
function viewCounties(i) {
    cityId = citys[i].id;
    $("body").data("cId", cityId);
    var nameOfCity = $.trim(citys[i].name);
    $("body").data("nameOfCity", nameOfCity);
    counties = [];
    var j = 0;
    $.each(areas,
        function (i, county) {
            if (county.cityId == cityId) {
                counties[j++] = county;
            }
        });
    countyTotalPage = Math.ceil(counties.length / p_pageSize);
    $(".provinceCity").find(".tabs").find("a").removeClass("current");
    $(".provinceCity .tabs").find("#county").addClass("current");
    $(".con .city .list a").removeClass("current");
    $(".con .city .list a[id='" + cityId + "']").addClass("current");
    $(".provinceCity").find(".con").children().hide();
    $(".provinceCity").find(".con").find(".county").show();
    countyPage(1);
}
function countyPage(page) {
    var nameValue = $("input.current1").attr("name");
    var nameOfProvince = $("body").data("pName");
    var cityCurName = $("body").data("nameOfCity");
    $("input.current1").removeClass("iGrays");
    $("input.current1").val(nameOfProvince + "-" + cityCurName);
    $(".county .list ul li").remove();
    if (page == 1) {
        $(".county .pre a").removeClass("can");
    } else {
        $(".county .pre a").addClass("can");
    }
    var start;
    var end;
    if (page <= 1) {
        page = 1;
        $(".county .pre a").removeClass("can");
        $(".county .next a").addClass("can");
    }
    if (countyTotalPage == 1) {
        $(".county .next a").removeClass("can");
        $(".county .pre a").removeClass("can");
    }
    if (page >= countyTotalPage) {
        page = countyTotalPage;
        $(".county .next a").removeClass("can");
        start = (page - 1) * p_pageSize;
        end = counties.length;
    } else if (page == 1) {
        start = (page - 1) * p_pageSize;
        end = start + p_pageSize;
        $(".county .pre a").removeClass("can");
        $(".county .next a").addClass("can");
    } else {
        start = (page - 1) * p_pageSize;
        end = start + p_pageSize;
        $(".county .next a").addClass("can");
        $(".county .pre a").addClass("can");
    }
    for (var i = start; i < end; i++) {
        var c_id = counties[i].id;
        var countyName = counties[i].areaName.substr(0, 4);
        ;
        var li = $('<li><a href="javascript:onclick=addrInput(' + i + ')" id="' + c_id + '">' + countyName + '</a></li>');
        $(".county .list ul").append(li);
    }
    $(".county .list #countyPage").remove();
    $(".county .list").append("<label id='countyPage' style='display:none;'>" + page + "</label>");
}
function addrInput(i) {
    var countyId = $.trim(counties[i].id);
    $(".con .hotCity .list a input").removeClass("current");
    $(".con .hotCity .list a input[id='" + cityId + "']").addClass("current");
    $(".con .county .list a").removeClass("current");
    $(".con .county .list a[id='" + countyId + "']").addClass("current");
    proId = $("body").data("pId");
    cityId = $("body").data("cId");
    var p = null;
    $.each(provinces,
        function (i, province) {
            if (province.id == proId) {
                p = province.provinceName;
                return false;
            }
        });
    var c = null;
    $.each(cities,
        function (i, city) {
            if (city.id == cityId) {
                c = city.name;
                return false;
            }
        });
    var a = null;
    $.each(counties,
        function (i, county) {
            if (county.id == countyId) {
                a = county.areaName;
                return false;
            }
        });
    $("input.current1").removeClass("iGrays");
    $(".provinceCity").hide();
    var rtn = p + "-" + c + "-" + a;
    $("input.current1").val(rtn);
    $(".backifname").hide();
    var nameValue = $("input.current1").attr("name");
    if (nameValue == 'order.sdeptProCity') {
        $("#deptCityId").val(cityId);
        $("input[name='order.sdeptProCity']").trigger("change", [cityId, countyId]);
    }
    if (nameValue == 'consignor.deptProCity') {
        $("input[name='consignor.deptProCity']").trigger("change", [cityId, countyId]);
    }
    if (nameValue == 'template.sdeptProCity') {
        $("input[name='template.sdeptProCity']").trigger("change", [cityId, countyId]);
    }
}
function hotCityAddrInput(proCityId) {
    proId = proCityId.split(",")[0];
    cityId = proCityId.split(",")[1];
    var cityCurName = proCityId.split(",")[2];
    $("body").data("nameOfCity", cityCurName);
    $("body").data("pId", proId);
    $("body").data("cId", cityId);
    $.each(provinces,
        function (i, pro) {
            if (pro.id == proId) {
                $("body").data("pName", pro.provinceName);
            }
        });
    counties = [];
    var j = 0;
    $.each(areas,
        function (i, county) {
            if (county.cityId == cityId) {
                counties[j++] = county;
            }
        });
    countyTotalPage = Math.ceil(counties.length / p_pageSize);
    $(".provinceCity").find(".tabs").find("a").removeClass("current");
    $(".provinceCity .tabs").find("#county").addClass("current");
    $(".con .city .list a").removeClass("current");
    $(".con .city .list a[id='" + cityId + "']").addClass("current");
    $(".provinceCity").find(".con").children().hide();
    $(".provinceCity").find(".con").find(".county").show();
    $(".con .provinceAll .list a").removeClass("current");
    countyPage(1);
}
var allProvinces = null;
var allCities = null;
var allAreas = null;
var allProId = null;
var cityIdAll = null;
var provinceAllTotalPage = null;
var pa_pageSize = 12;
var pa_pageSize_Less = 11;
var pa_currentPage = 1;
function sendAllProvinceAjax() {
    allProvinces = jsonProvinces.provinces;
    $("body").data("allProvinces", allProvinces);
    viewAllProvince(1);

    /* $.ajax({
     type: "get",
     url: "" + webroot + "queryAllProvinces",
     async: false,
     dataType: "json",
     success: function (data) {
     allProvinces = data.provinces;
     $("body").data("allProvinces", allProvinces);
     viewAllProvince(1);
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert(textStatus);
     }
     });*/
}
function sendAllCitiesAjax() {
    allCities = jsonCites.cities;
    $("body").data("CitysAll", jsonCites);

    viewAllHotCities();
    /* $.ajax({
     type: "get",
     url: "" + webroot + "queryCities",
     async: false,
     dataType: "json",
     success: function (data) {
     allCities = data.cities;
     $("body").data("CitysAll", data);
     viewAllHotCities();
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert(textStatus);
     }
     });*/
}
function sendAllCountiesAjax() {
    allAreas = jsonAreas.areas;
    $("body").data("allCountys", allAreas);

    /*  $.ajax({
     type: "get",
     url: "" + webroot + "queryAllAreas",
     async: false,
     dataType: "json",
     success: function (data) {
     allAreas = data.areas;
     $("body").data("allCountys", data.areas);
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert("网络繁忙，请稍后再试！");
     }
     });*/
}
function viewAllHotCities() {
    $.each(allCities,
        function (i, city) {
            if (city.hotCity) {
                $(".hotCityAll .list ul").append("<li><a><input type='button' style='background:none;border:0px;cursor: pointer;' onclick=hotCityAddrInputAll(\'" + city.provinceId + "," + city.id + "," + city.name + "\') id='" + city.id + "' value='" + city.name + "'></a></li>");
            }
        });
}
$(".provinceAll .pre a").bind('click',
    function () {
        var provincePage1 = parseInt($('#provincePage1').html());
        if (provincePage1 == 1) {
            return;
        }
        viewAllProvince(provincePage1 - 1);
    });
$(".cityAll .pre a").bind('click',
    function () {
        var cityPages1 = parseInt($('#cityPage1').html());
        if (cityPages1 == 1) {
            return;
        }
        allCityPage(cityPages1 - 1);
    });
$(".countyAll .pre a").bind('click',
    function () {
        var countyPages = parseInt($('#countyPage1').html());
        if (countyPages == 1) {
            return;
        }
        allCountyPage(countyPages - 1);
    });
$(".provinceAll .next a").bind('click',
    function () {
        var provincePage1 = parseInt($('#provincePage1').html());
        provinceAllTotalPage = countAllProvincePages();
        if (provincePage1 >= provinceAllTotalPage) {
            return;
        }
        viewAllProvince(provincePage1 + 1);
    });
$(".cityAll .next a").bind('click',
    function () {
        if ($(this).hasClass("can")) {
            var cityPages1 = parseInt($('#cityPage1').html());
            allCityPage(cityPages1 + 1);
        }
    });
$(".countyAll .next a").bind('click',
    function () {
        if ($(this).hasClass("can")) {
            var countyPages = parseInt($('#countyPage1').html());
            allCountyPage(countyPages + 1);
        }
    });
function json2str(o) {
    var arr = [];
    var fmt = function (s) {
        if (typeof s == 'object' && s != null) return json2str(s);
        return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
    };
    for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
    return '{' + arr.join(',') + '}';
}
function countAllProvincePages() {
    provinceAllTotalPage = Math.ceil(allProvinces.length / pa_pageSize);
    return provinceAllTotalPage;
}
function viewAllProvince(page) {
    $(".provinceAll .list ul li").remove();
    if (page == 1) {
        $(".provinceAll .pre a").removeClass("can");
        $(".provinceAll .next a").addClass("can");
    } else {
        $(".provinceAll .pre a").addClass("can");
        $(".provinceAll .next a").addClass("can");
    }
    var end;
    var start;
    if(allProvinces.length<12){
        pa_pageSize=allProvinces.length;
    }else{
        pa_pageSize=12;
    }
    if (page == provinceAllTotalPage) {
        start = (page - 1) * pa_pageSize;
        end = allProvinces.length;
        $(".provinceAll .next a").removeClass("can");
    } else {
        start = (page - 1) * pa_pageSize;
        end = start + pa_pageSize;
    }
    var li="";
    $(".provinceAll .list ul").empty();
    for (var i = start; i < end; i++) {
        var p_id = allProvinces[i].id;
        var p_name = allProvinces[i].provinceName;
        if (allProvinces[i].provinceName == '内蒙古自治区') {
            p_name = '内蒙古';
        } else if (allProvinces[i].provinceName == '黑龙江省') {
            p_name = '黑龙江';
        } else {
            p_name = allProvinces[i].provinceName.substr(0, 2);
        }
        li+= '<li><a style="background: none repeat scroll 0% 0% transparent; border: 0px none;" href="javascript:onclick=viewAllCities(' + i + ');" id="' + p_id + '">' + p_name + '</a></li>';
    }
    if(li.length>0){
        $(".provinceAll .list ul").append(li);
    }

    $(".provinceAll .list #provincePage1").remove();
    $(".provinceAll .list").append("<label id='provincePage1' style='display:none;'>" + page + "</label>");
}
function viewAllCities(i) {
    if($("#txtclear").val()!="0"){//从全部省中点击，清空市模糊查询
        $("#txtcity").val("");
        allCities = jsonCites.cities;
    }
    $("#txtclear").val("");
    allProId = allProvinces[i].id;
    $("#provinceId").val(allProId);
    $("body").data("pAllName", allProvinces[i].provinceName);
    $("body").data("pAllId", allProId);



    if($("input.current2").attr("allowzone") == "province"){
        $("input.current2").val(allProvinces[i].provinceName);
        $("input.current2").attr("value", allProvinces[i].provinceName);
        //记录省代码
        $("input.current2").attr("regioncode",allProId);
        $("input.current2").attr("regionzone","province");
    }
    var j;
    if(allCitys.length>0){
        if(allCitys[0].id!="000000"){
            allCitys[0]={
                "name": "不限",
                "id": "000000",
                "cityPinyin": "",
                "lastModifyTime": null,
                "provinceId": allProId,
                "hotCity": false,
                "cityShortPY": "null"
            };
        }
        if(allCitys.length>1){
            allCitys=[];
            allCitys[0]={
                "name": "不限",
                "id": "000000",
                "cityPinyin": "",
                "lastModifyTime": null,
                "provinceId": allProId,
                "hotCity": false,
                "cityShortPY": "null"
            };
        }
        j = 1;
    }else{
        j = 0;
    }
    var tempAllCitys=[];
    var z=0;

    $.each(allCities,
        function (i, city) {
            if (city.provinceId == allProId) {
                allCitys[j++] = city;
                tempAllCitys[z++] = city;
            }
        });

    if(allCitys.length>0&&tempAllCitys.length==1){
        allCitys=tempAllCitys;
    }

    if(allCitys.length<12){
        pa_pageSize=allCitys.length;
    }else{
        pa_pageSize=12;
    }
    allCityTotalPage = Math.ceil(allCitys.length / pa_pageSize);
    $(".provinceCityAll").find(".tabs").find("a").removeClass("current");
    $(".provinceCityAll .tabs").find("#cityAll").addClass("current");
    $(".con .provinceAll .list a").removeClass("current");
    $(".con .provinceAll .list a[id='" + allProId + "']").addClass("current");
    $(".provinceCityAll").find(".con").children().hide();
    $(".provinceCityAll").find(".con").find(".cityAll").show();
    allCityPage(1);
}

function allCityPage(page) {
    $(".cityAll .list ul li").empty();
    $(".cityAll .list ul li").remove();
    if (page == 1) {
        $(".cityAll .pre a").removeClass("can");
    } else {
        $(".cityAll .pre a").addClass("can");
    }
    var start;
    var end;
    if (page <= 1) {
        page = 1;
        $(".cityAll .pre a").removeClass("can");
        $(".cityAll .next a").addClass("can");
    }
    if (allCityTotalPage == 1) {
        $(".cityAll .next a").removeClass("can");
        $(".cityAll .pre a").removeClass("can");
    }
    if (page >= allCityTotalPage) {
        page = allCityTotalPage;
        $(".cityAll .next a").removeClass("can");
        start = (page - 1) * pa_pageSize;
        end = allCitys.length;
    } else if (page == 1) {
        start = (page - 1) * pa_pageSize;
        end = start + pa_pageSize;
        $(".cityAll .pre a").removeClass("can");
        $(".cityAll .next a").addClass("can");
    } else {
        start = (page - 1) * pa_pageSize;
        end = start + pa_pageSize;
        $(".cityAll .next a").addClass("can");
        $(".cityAll .pre a").addClass("can");
    }
    for (var i = start; i < end; i++) {
        var c_id = allCitys[i].id;
        var cityName = allCitys[i].name.substr(0, 4);
        if(c_id==000000){
            var li = $('<li><a href="javascript:onclick=addrInputAll(1,' + i + ')" id="' + c_id + '">' + cityName + '</a></li>');
        }else{
            var li = $('<li><a href="javascript:onclick=viewAllCounties(' + i + ')" id="' + c_id + '">' + cityName + '</a></li>');
        }
        $(".cityAll .list ul").append(li);
    }
    $(".cityAll .list #cityPage1").remove();
    $(".cityAll .list").append("<label id='cityPage1' style='display:none;'>" + page + "</label>");
}
function viewAllCounties(i) {
    if($("#txtclear1").val()!="0"){
        allAreas=jsonAreas.areas;
        $("#txtarea").val("");
    }
    $("#txtclear1").val("");

    cityIdAll = allCitys[i].id;
    $("#cityId").val(cityIdAll);
    $("body").data("cAllId", cityIdAll);
    var cityname = $.trim(allCitys[i].name);
    $("body").data("nameOfCityAll", cityname);

    var j;
    if(countiesAll.length>0){
        if(countiesAll[0].id!="000000"){
            countiesAll[0]={
                "id": "000000",
                "cityId": cityIdAll,
                "cityName": "不限",
                "areaName": "不限",
                "updateTime": "1422613080612",
                "provinceId": 000000,
                "pinYin": "null",
                "pinYinChar": "null",
                "isShowWithCity": "0"
            }
        }
        j = 1;
    }else{
        j = 0;
    }

    $.each(allAreas,
        function (i, countys) {
            if (countys.cityId == cityIdAll) {
                countiesAll[j++] = countys;
            }
        });
    if(countiesAll.length<12){
        pa_pageSize=countiesAll.length;
    }else{
        pa_pageSize=12;
    }
    countyTotalPageAll = Math.ceil(countiesAll.length / pa_pageSize);
    $(".provinceCityAll").find(".tabs").find("a").removeClass("current");
    $(".provinceCityAll .tabs").find("#countyAll").addClass("current");
    $(".con .cityAll .list a").removeClass("current");
    $(".con .cityAll .list a[id='" + cityIdAll + "']").addClass("current");
    $(".provinceCityAll").find(".con").children().hide();
    $(".provinceCityAll").find(".con").find(".countyAll").show();
    allCountyPage(1);
}
function
allCountyPage(page) {
    var nameOfProvince = $("body").data("pAllName");
    var cityCurrentName = $("body").data("nameOfCityAll");
    var idOfCity =$("body").data("cAllId");
    //记录地区代码
    if($("input.current2").attr("allowzone") == "city" || $("input.current2").attr("allowzone") == "province"){

        $("input.current2").removeClass("iGrays");
        $("input.current2").val(nameOfProvince + "-" + cityCurrentName);

        $("input.current2").attr("regioncode",idOfCity);
        $("input.current2").attr("regionzone","city");
    }
    $(".countyAll .list ul li").remove();
    if (page == 1) {
        $(".countyAll .pre a").removeClass("can");
    } else {
        $(".countyAll .pre a").addClass("can");
    }
    var start;
    var end;
    if (page <= 1) {
        page = 1;
        $(".countyAll .pre a").removeClass("can");
        $(".countyAll .next a").addClass("can");
    }
    if (countyTotalPageAll == 1) {
        $(".countyAll .next a").removeClass("can");
        $(".countyAll .pre a").removeClass("can");
    }
    if (page >= countyTotalPageAll) {
        page = countyTotalPageAll;
        $(".countyAll .next a").removeClass("can");
        start = (page - 1) * pa_pageSize;
        end = countiesAll.length;
    } else if (page == 1) {
        start = (page - 1) * pa_pageSize;
        end = start + pa_pageSize;
        $(".countyAll .pre a").removeClass("can");
        $(".countyAll .next a").addClass("can");
    } else {
        start = (page - 1) * pa_pageSize;
        end = start + pa_pageSize;
        $(".countyAll .next a").addClass("can");
        $(".countyAll .pre a").addClass("can");
    }

    for (var i = start; i < end; i++) {
        var c_id = countiesAll[i].id;
        var countyName = countiesAll[i].areaName.substr(0, 4);
        ;
        var li = $('<li><a href="javascript:onclick=addrInputAll(2,' + i + ')" id="' + c_id + '">' + countyName + '</a></li>');
        $(".countyAll .list ul").append(li);
    }
    $(".countyAll .list #countyPage1").remove();
    $(".countyAll .list").append("<label id='countyPage1' style='display:none;'>" + page + "</label>");
}
function addrInputAll(type,i) {
    if(type==0){

    }else if(type==1){
        //记录地区代码
        $("input.current2").attr("regioncode",allProId);
        $("input.current2").next().val(allProId);
        $("input.current2").attr("regionzone","district");
        $(".con .hotCityAll .list a input").removeClass("current");
        $(".con .hotCityAll .list a input[id='" + cityIdAll + "']").addClass("current");
        $(".con .countyAll .list a").removeClass("current");
        $(".con .countyAll .list a[id='" + countyId + "']").addClass("current");
        allProId = $("body").data("pAllId");
        cityIdAll = $("body").data("cAllId");
        var p = null;
        $.each(allProvinces,
            function (i, province) {
                if (province.id == allProId) {
                    p = province.provinceName;
                    return false;
                }
            });
        var c = "不限";
        var nameValue = $("input.current2");
        nameValue.removeClass("iGrays");
        $(".provinceCityAll").hide();
        var rtn = p + "-" + c;
        $("input.current2").val(rtn);
        $(".backifname").hide();
        var nameValue = $("input.current2").attr("name");
        if (nameValue == "consignor.addrProCity") {
            $("#provinceId").val(allProId);
            $("#cityId").val(000000);
        }
        if (nameValue == "order.caddrProCity") {
            $("input[name='order.caddrProCity']").trigger("change");
        }
        if (nameValue == "consigneeInfo.addrProCity") {
            $("input[name='consigneeInfo.addrProCity']").trigger("change");
        }
        if (nameValue == 'template.caddrProCity') {
            $("input[name='template.caddrProCity']").trigger("change");
        }
    }else{
        var countyId = $.trim(countiesAll[i].id);
        //记录地区代码
        if(countyId==000000){
            $("input.current2").attr("regioncode",cityIdAll);
            $("input.current2").next().val(cityIdAll);
        }else{
            $("input.current2").attr("regioncode",countyId);
            $("input.current2").next().val(countyId);
        }
        $("input.current2").attr("regionzone","district");
        $(".con .hotCityAll .list a input").removeClass("current");
        $(".con .hotCityAll .list a input[id='" + cityIdAll + "']").addClass("current");
        $(".con .countyAll .list a").removeClass("current");
        $(".con .countyAll .list a[id='" + countyId + "']").addClass("current");
        allProId = $("body").data("pAllId");
        cityIdAll = $("body").data("cAllId");
        var p = null;
        $.each(allProvinces,
            function (i, province) {
                if (province.id == allProId) {
                    p = province.provinceName;
                    return false;
                }
            });
        var c = null;
        $.each(allCities,
            function (i, city) {
                if (city.id == cityIdAll) {
                    c = city.name;
                    return false;
                }
            });
        var a = null;
        $.each(countiesAll,
            function (i, county) {
                if (county.id == countyId) {
                    a = county.areaName;
                    return false;
                }
            });
        var nameValue = $("input.current2");
        nameValue.removeClass("iGrays");
        $(".provinceCityAll").hide();
        var rtn = p + "-" + c + "-" + a;
        $("input.current2").val(rtn);
        $(".backifname").hide();
        var nameValue = $("input.current2").attr("name");
        if (nameValue == "consignor.addrProCity") {
            $("#provinceId").val(allProId);
            $("#cityId").val(cityIdAll);
        }
        if (nameValue == "order.caddrProCity") {
            $("input[name='order.caddrProCity']").trigger("change");
        }
        if (nameValue == "consigneeInfo.addrProCity") {
            $("input[name='consigneeInfo.addrProCity']").trigger("change");
        }
        if (nameValue == 'template.caddrProCity') {
            $("input[name='template.caddrProCity']").trigger("change");
        }
    }
}
function hotCityAddrInputAll(proCityId) {
    allProId = proCityId.split(",")[0];
    cityIdAll = proCityId.split(",")[1];
    var cityCurName = proCityId.split(",")[2];
    $("body").data("nameOfCityAll", cityCurName);
    $("body").data("pAllId", allProId);
    $("body").data("cAllId", cityIdAll);
    $("#cityId").val(cityIdAll);
    $.each(allProvinces,
        function (i, pro) {
            if (pro.id == allProId) {
                $("body").data("pAllName", pro.provinceName);
            }
        });
    countiesAll = [];
    countiesAll.push({
        "id": "000000",
        "cityId": cityIdAll,
        "cityName": "不限",
        "areaName": "不限",
        "updateTime": "1422613080612",
        "provinceId": 000000,
        "pinYin": "null",
        "pinYinChar": "null",
        "isShowWithCity": "0"
    });
    var j = 1;


    $.each(allAreas,
        function (i, county) {
            if (county.cityId == cityIdAll&&county.id!="000000") {
                countiesAll[j++] = county;
            }
        });
    pa_pageSize=12;
    countyTotalPageAll = Math.ceil(countiesAll.length / pa_pageSize);
    $(".provinceCityAll").find(".tabs").find("a").removeClass("current");
    $(".provinceCityAll .tabs").find("#countyAll").addClass("current");
    $(".con .cityAll .list a").removeClass("current");
    $(".con .cityAll .list a[id='" + cityIdAll + "']").addClass("current");
    $(".provinceCityAll").find(".con").children().hide();
    $(".provinceCityAll").find(".con").find(".countyAll").show();
    $(".con .provinceAll .list a").removeClass("current");
    allCountyPage(1);
}

function selectAll(){
    $(".provinceCityAll").hide();
}


/**
 * 发货源地址栏模糊查询
 * @param obj 输入框的值
 * @param flag 区分是省（0），市（1），区（2）
 */

function fnSearchAddress(obj,flag){
    $obj=$(obj);
    var address = $.trim($obj.val());
    getADRInfo(flag,address);
}

function getADRInfo(flag,address){
    var citys1 =jsonCites.cities;
    var area1 =jsonAreas.areas;
    if(flag==0){
        var provinces1 =jsonProvinces.provinces;
        var provinces=[];
        $.each(provinces1,function(i,province){
            var name=province.provinceName;
            if(address&&name.indexOf(address)!=-1){
                provinces.push(province);
            }
        });
        if(provinces.length>0){
            allProvinces=provinces;
            viewAllProvince(1);
        }
        if(address==""){
            allProvinces=provinces1;
            allCities=citys1;
            allAreas=area1;
            viewAllProvince(1);
            $("#txtcity").val("");
            $("#txtclear").val("");//清空市模糊查询标识
        }
    }

    if(flag==1){
        allCitys = [];
        var citys=[];
        var proId=$("#provinceId").val();
        allCitys.push({
            "name": "不限",
            "id": "000000",
            "cityPinyin": "",
            "lastModifyTime": null,
            "provinceId": proId,
            "hotCity": false,
            "cityShortPY": "null"
        });

        var citys1Str = "["+JSON.stringify({
                "name": "不限",
                "id": "000000",
                "cityPinyin": "",
                "lastModifyTime": null,
                "provinceId": proId,
                "hotCity": false,
                "cityShortPY": "null"
            })+","+ JSON.stringify(citys1).replace("[","").replace("]","")+"]"

        citys1 = eval(citys1Str);
        $.each(citys1,function(i,city){
            var name=city.name;
            if(address&&city.provinceId==proId&&name.indexOf(address)!=-1){
                citys.push(city);
            }
        });

        if(citys.length>0){
            allCities=citys;
            $("#txtclear").val("0");//清空市模糊查询标识
            allCitys = [];
            $.each(allProvinces,function(i,pro){
                if(pro.id==proId){
                    viewAllCities(i)
                    return;
                }
            });
        }
        if(address==""){
            allCities=citys1;
            $.each(allProvinces,function(i,pro){
                if(pro.id==proId){
                    viewAllCities(i);
                    return;
                }
            });
        }
    }
    if(flag==2){
        countiesAll=[];
        var areas=[];
        var cityId=$("#cityId").val();
        var area1Str = "["+JSON.stringify({
                "id": "000000",
                "cityId": cityId,
                "cityName": "不限",
                "areaName": "不限",
                "updateTime": "1422613080612",
                "provinceId": proId,
                "pinYin": "null",
                "pinYinChar": "null",
                "isShowWithCity": "0"
            })+","+ JSON.stringify(area1).replace("[","").replace("]","")+"]"

        area1 = eval(area1Str);
        countiesAll.push({
            "id": "000000",
            "cityId": cityId,
            "cityName": "不限",
            "areaName": "不限",
            "updateTime": "1422613080612",
            "provinceId": proId,
            "pinYin": "null",
            "pinYinChar": "null",
            "isShowWithCity": "0"
        });

        $.each(area1,function(i,area){
            var name=area.areaName;
            if(address&&area.cityId==cityId&&name.indexOf(address)!=-1){
                areas.push(area);
            }
        });
        if(areas.length>0){
            allAreas=areas;
            countiesAll=[];
            $("#txtclear1").val("0");//清空区县模糊查询标识
            $.each(allCitys,function(i,ci){
                if(ci.id==cityId){
                    viewAllCounties(i)
                    return;
                }
            });
        }
        if(address==""){
            allAreas=area1;
            $.each(allCitys,function(i,ci){
                if(ci.id==cityId){
                    viewAllCounties(i)
                    return;
                }
            });
        }
    }
}